package com.session02;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class TestDateTime {
    public static void main(String[] args) {
//        long before = System.currentTimeMillis();
//        int sum = 0;
//        for(int i = 0; i < 10000; i++){
//            System.out.println(i);
//        }
//        long after = System.currentTimeMillis();
//        System.out.println(after - before);


//        Date date = new Date();//直接获取到一个表示当前时间日期的Date对象
//        System.out.println(date);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
//        String dateStr = sdf.format(date);
//        System.out.println(dateStr);




//        Calendar cal = new GregorianCalendar();//格里高利历
//        cal = Calendar.getInstance();//获取到一个表示当前时间日期的Calendar对象
//        int year = 1999,month=12,day=3;
//        cal.set(year,month - 1,day);
//        Date birthday = cal.getTime();
//        String birthStr = sdf.format(birthday);
//        System.out.println(birthStr);

        //1、在java.time包中，表示瞬时的有一个新的类Instant
        //这个类可以把时间瞬时精确到纳秒
        Instant now = Instant.now();
        System.out.println(now.getNano());

        //2、LocalDate  LocalTime LocalDateTime
        //2-1、获取当前对象
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);

        //2-2、获取指定的时间日期对象
        localDate = LocalDate.of(1999,12,3);
        localTime = LocalTime.of(10,30,35);
        localDateTime = LocalDateTime.of(1999,12,3,10,30,35);

        //2-3、差值计算
        localDate = localDate.plusDays(3);
        localDate = localDate.minusMonths(10);
        System.out.println(localDate);

        System.out.println("-------------------------------");
        //2-4、格式化字符串
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss E");
        localDateTime = LocalDateTime.now();
        String description = localDateTime.format(formatter);
        System.out.println(description);
    }
}
